<template>
	<div class="field-control" :class="{'field-control-error': !validate}">
		<Input ref="input" :size="size" :disabled="disabled" @on-keyup.enter="inputEnter" :name="name" :type="inputType" @input="valueInput" :value="value" />
		<div class="field-message" v-show="!validate">{{message}}</div>
	</div>
</template>

<script>
import Helper from "store/helper";
import $ from "@/common";
var messageText = {
  require: "${name}不能为空",
  format: "${type}格式不正确",
  exp: "请输入正确的${name}"
};
var regType = {
  mobile: /^(1[3-9][0-9])[0-9]{8}$/,
  email: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
  host: /0\d{2,3}-\d{7,8}|(^[0 -9]{3,8}$)|(^[0−9]3,4[0 -9]{3,8}$)|^(1[3-9][0-9])[0-9]{8}$/,
  // seatCode: /^[0-9]+@[A-Za-z]+$/
  seatCode: /.+/
};
export default {
  model: {
    prop: "value",
    event: "input"
  },
  props: {
    title: {},
    size: {
      default: null
    },
    name: {},
    model: {},
    rowIndex: {
      default: null
    },
    rowLength: {
      default: null
    },
    status: {
      default() {
        return {};
      }
    },
    value: {
      default: null
    },
    //可传入的行数据
    rowData: {},
    //指定初始渲染值
    renderValue: {},
    type: {},
    hasValidate: {
      default: true
    },
    enable: {
      default: true
    },
    require: {
      default: false
    },
    validator: {
      default: false
    },
    forceEnable: {
      default: null
    },
    parentData: {},
    searchMode: {
      default: false
    }
  },
  data() {
    return {
      data: null,
      typeName: "数据",
      //首次再加
      fristLoad: true,
      //附加数据
      attachData: null,
      message: null,
      //input中使用的type
      inputType: null,
      maxLenth: null,
      minLength: null,
      regExp: null,
      tips: null
    };
  },
  watch: {},
  computed: {
    validate() {
      var v = false;
      if (this.status.enable === false || this.status.show === false) {
        this.$emit("validate", true);
        v = true;
      }
      if (!this.hasValidate && !v) {
        this.$emit("validate", true);
        v = true;
      }
      if (((!this.validator && this.fristLoad) || this.disabled) && !v) {
        this.message = null;
        this.$emit("validate", true);
        v = true;
      }
      //非空验证
      if (!v) {
        //noRequire属性设置表示值类型不进行非空验证
        if (
          (this.require || this.model.required) &&
          this.valueNull &&
          !this.noRequire
        ) {
          this.message = messageText.require.replace(
            "${name}",
            this.model.name
          );
          this.$emit("validate", false);
          return false;
        } else if (
          this.regExp &&
          !this.valueNull &&
          !this.value.match(this.regExp)
        ) {
          this.message = messageText.exp.replace("${name}", this.model.name);
          this.$emit("validate", false);
          return false;
        }

        v = true;
        var message = null;
        if (this.rangeset.max) {
          var max = $.composeValue(this.rangeset.max, {
            ...this.rowData,
            parent: this.parentData
          });
          max = parseFloat(max);
          if (!isNaN(max) && parseFloat(this.value) > max) {
            message = `请输入小于${max}的数值`;
            v = false;
          }
        }
        if (this.rangeset.min) {
          var min = $.composeValue(this.rangeset.min, {
            ...this.rowData,
            parent: this.parentData
          });
          min = parseFloat(min);
          if (!isNaN(min) && parseFloat(this.value) < min) {
            message = `请输入大于${min}的数值`;
            v = false;
          }
        }
        //额外正则类型验证
        if (this.rangeset.type && regType[this.rangeset.type]) {
          try {
            let m = this.value.match(regType[this.rangeset.type]);
            if (!m) {
              v = false;
              message = messageText.exp.replace("${name}", this.model.name);
            }
          } catch (ex) {
            console.warn(ex);
          }
        }

        if (this.rangeset.reg_exp) {
          try {
            let m = this.value.match(this.rangeset.reg_exp);
            if (!m) {
              v = false;
              message = messageText.exp.replace("${name}", this.model.name);
            }
          } catch (ex) {
            console.warn(ex);
          }
        }
        if (this.model.validate) {
          let validates = JSON.parse(this.model.validate);
          for (let s of validates) {
            if (s.type === "max") {
              let max = $.composeValue(s.max, this.parentData);
              if (this.value > max) {
                message = s.msg;
				v = false;
				break;
              }
            }
          };
        }

        this.message = message;
        this.$emit("validate", v);
        return v;
      }
      return v;
    },
    rangeset() {
      var rangeset = this.model.rangeset;
      if (!rangeset) return {};
      if (typeof rangeset === "object") {
        return rangeset;
      } else {
        var res = {};
        try {
          res = JSON.parse(rangeset) || {};
        } catch (ex) {
          res = {};
        }
        return res;
      }
    },
    ///控制判断
    valueNull() {
      return (
        this.value === null || this.value === undefined || this.value === ""
      );
    },
    disabled() {
      if (this.forceEnable !== null) {
        return !this.forceEnable;
      } else if (this.status.enable === false) {
        return true;
      } else {
        return this.model.editable === false;
      }
    }
  },
  methods: {
    inputEnter() {
      this.$emit("enter");
    },
    doValidate() {
      var vaild = true;
      return vaild;
    },
    valueInput(v) {
      this.$emit("input", v);
      this.fristLoad = false;
      //this.validate()
    },
    focus(v) {
      if (this.$refs.input && this.$refs.input.focus()) {
        this.$refs.input.focus();
      }
    },
    upForm() {
      return Helper.getParentStore(this);
    }
  }
};
</script>

<style lang="less">
</style>


